package com.zzjc.web.controller;

import com.zzjc.web.entity.Device;
import com.zzjc.web.service.DeviceService;
import com.zzjc.web.util.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation("添加设备信息")
    @PostMapping("/addDevice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id,(序列号)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "deviceName", value = "设备名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型（1-摄像头；2-刻录机；3-手环；4-网关；5-平板；6-服务器）", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "poolName", value = "所属泳池名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "在线状态（在线1，不在线0）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "channelNo", value = "通道数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "rowNum", value = "所属行", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "colNum", value = "所属列", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "enabled", value = "启用状态（启用1，不启用0）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "description", value = "描述", required = false, dataType = "String"),
            @ApiImplicitParam(name = "videoUrl", value = "播放地址", required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraIp", value = "相机IP", required = false, dataType = "String")
    })
    public ApiResponse<?> addDevice(
            @RequestParam(value = "deviceId", required = true) String deviceId,
            @RequestParam(value = "deviceName", required = true) String deviceName,
            @RequestParam(value = "deviceType", required = true) Integer deviceType,
            @RequestParam(value = "poolName", required = false) String poolName,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "channelNo", required = false) Integer channelNo,
            @RequestParam(value = "rowNum", required = false) Integer rowNum,
            @RequestParam(value = "colNum", required = false) Integer colNum,
            @RequestParam(value = "enabled", required = false) Integer enabled,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "videoUrl", required = false) String videoUrl,
            @RequestParam(value = "cameraIp", required = false) String cameraIp) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setPoolName(poolName);
        device.setStatus(status);
        device.setChannelNo(channelNo);
        device.setRowNum(rowNum);
        device.setColNum(colNum);
        device.setEnabled(enabled);
        device.setDescription(description);
        device.setVideoUrl(videoUrl);
        device.setCameraIp(cameraIp);
        return deviceService.addDevice(device);
    }

    @ApiOperation("更新设备信息(序列号DeviceId约定为不可修改)")
    @PostMapping("/updateDevice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID（序列号）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "deviceName", value = "设备名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型（1-摄像头；2-刻录机；3-手环；4-网关；5-平板；6-服务器）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "poolName", value = "所属泳池名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "在线状态（在线1，不在线0）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "channelNo", value = "通道数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "rowNum", value = "所属行", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "colNum", value = "所属列", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "enabled", value = "启用状态（启用1，不启用0）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "description", value = "描述", required = false, dataType = "String"),
            @ApiImplicitParam(name = "videoUrl", value = "播放地址", required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraIp", value = "相机IP", required = false, dataType = "String")
    })
    public ApiResponse<?> updateDevice(
            @RequestParam(value = "deviceId", required = true) String deviceId,
            @RequestParam(value = "deviceName", required = false) String deviceName,
            @RequestParam(value = "deviceType", required = false) Integer deviceType,
            @RequestParam(value = "poolName", required = false) String poolName,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "channelNo", required = false) Integer channelNo,
            @RequestParam(value = "rowNum", required = false) Integer rowNum,
            @RequestParam(value = "colNum", required = false) Integer colNum,
            @RequestParam(value = "enabled", required = false) Integer enabled,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "videoUrl", required = false) String videoUrl,
            @RequestParam(value = "cameraIp", required = false) String cameraIp) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setPoolName(poolName);
        device.setStatus(status);
        device.setChannelNo(channelNo);
        device.setRowNum(rowNum);
        device.setColNum(colNum);
        device.setEnabled(enabled);
        device.setDescription(description);
        device.setVideoUrl(videoUrl);
        device.setCameraIp(cameraIp);
        return deviceService.updateDevice(device);
    }

    @ApiOperation("删除设备")
    @PostMapping("/deleteDevice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceList", value = "设备id集合", dataType = "String", required = true)
    })
    public ApiResponse deleteDevice(
            @RequestParam(value = "deviceList", required = true) List<String> deviceList) {
        return deviceService.deleteDevice(deviceList);
    }

    @ApiOperation("获取设备列表")
    @PostMapping("/listDevice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "String", required = false),
            @ApiImplicitParam(name = "deviceName", value = "设备名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "deviceType", value = "设备类型", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "poolName", value = "所属泳池名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageNum", value = "当前页index", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面数据条数", dataType = "Integer", required = true)
    })
    public ApiResponse getAllDevices(
            @RequestParam(value = "deviceId", required = false) String deviceId,
            @RequestParam(value = "deviceName", required = false) String deviceName,
            @RequestParam(value = "deviceType", required = false) Integer deviceType,
            @RequestParam(value = "poolName", required = false) String poolName,
            @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setPoolName(poolName);
        device.setPageNum(pageNum);
        device.setPageSize(pageSize);
        return deviceService.getAllDevices(device);
    }
} 